﻿package com.sff.site.tree {
	import com.sff.core.sff_internal;
	import com.sff.site.core.IPath;
	import com.sff.site.node.NodeDescriptor;

	public class TreeDescriptor extends NodeDescriptor {
		
		
		public function get path() : IPath {
			return _path;
		}
		
		public function TreeDescriptor( datas : XML ) {
			super( datas );
		}
		
		override protected function parse( datas : XML) : void {
			super.parse( datas );
			
			_path = new SiteTreePath(datas.@path.toString());
			
			
			//CONSTANTS::release
			{
			if( ! _path.nodeExist() )
				throw new Error( "Path doesn't exist in site tree : " + _path );
			}
			
			
			//CONSTANTS::debug
			{
			if( ! _path.nodeExist() ) {
				
				var t : SiteTree = TreesProvider.sff_internal::getTree( _path.getDevice() );
				var txml : XML = <url_config path="/"/>;
				var curnode : XML = txml;
				var anode : XML;
				for (var i : int = 0; i < _path.segments().length; i++) {
					anode = <node id={_path.segments()[i]}/>;
					curnode.appendChild( anode );
					curnode = anode;
				}
				
				
				var desc : TreeDescriptor = new TreeDescriptor( txml );
				TreesProvider.sff_internal::injectTreeDescription( desc );
			}
			}
		}

		override protected function validate(datas : XML) : void {
			if( datas.node.length() == 0 )
				throw new Error( "TreeDescriptor - validate : no node tag in input datas" );
		}

		override protected function grabId(datas : XML) : void {
			// url_config has no id defined
			if( datas.@default.length() )
				_defaultId = datas.@default.toString();
		}
		
		private var _path : IPath;
		
	}
}
